package com.adtech.adms.mainview.web.action;

import java.io.IOException;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.file.pojo.FileInfo;
import com.adtech.adms.commons.tree.TreeNode;
import com.adtech.adms.system.pojo.Feature;
import com.adtech.adms.system.pojo.User;
import com.adtech.adms.system.service.FeatureService;
import com.adtech.core.web.action.BaseAction;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opensymphony.xwork2.conversion.annotations.TypeConversion;

/**
 * 平台框架
 * @author Dormin 
 * @since 2011-6-3 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class MainAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private FeatureService featureService;

	public void setFeatureService(FeatureService featureService) {
		this.featureService = featureService;
	}

	private List<TreeNode<Feature>> nodes;
	
	private User user;
	
	private String menuId;
	
	private String navDetail;
	
	private String newPassword;
	
	private String oldPassword;
	
	
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getNavDetail() {
		return navDetail;
	}

	public void setNavDetail(String navDetail) {
		this.navDetail = navDetail;
	}

	public List<TreeNode<Feature>> getNodes() {
		return nodes;
	}

	public void setNodes(List<TreeNode<Feature>> nodes) {
		this.nodes = nodes;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	/**
	 * @Function 获取详细信息
	 * @param
	 * @return String
	 * @exception
	 * 
	 * @author Dormin
	 * @changed
	 * 
	 */
	public String getDetail() {
		user = (User) session.get(SessionKey.SESSION_KEY_USER);
		nodes = featureService.getTreeNodeListByFeatures(user.getFeaturesTree());
//		JSONArray jsonArray2 = JSONArray.fromObject( nodes );      
//        System.out.println("Hymantest:"+ jsonArray2.toString() );    
//        navDetail = jsonArray2.toString();
        ObjectMapper map = new ObjectMapper();//这是jackson的类库
        try {
			navDetail= map.writeValueAsString(nodes);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       // prints ["first","second"]    

		return SUCCESS;
	}
	


	public String getThirMenu() {
		nodes = featureService.getFeatureTree(this.getCurrentUser(),new Long(menuId));
		return SUCCESS;
	}
	
	public String toWord() throws IOException {
		/*
		String path= "d:/asdf.doc";
		String content ="asdasdasd";
		byte b[] = content.getBytes();
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		POIFSFileSystem fs = new POIFSFileSystem();
		DirectoryEntry directory = fs.getRoot();
		directory.createDocument("WordDocument", bais);
		FileOutputStream ostream = new FileOutputStream(path);
		fs.writeFilesystem(ostream);
		bais.close();
		ostream.close();
		*/
		// 如果要导出复杂的word， 将定义好word模板，然后替换里面的标签文字
		
		
		
		

		return SUCCESS;
	}

	@Override
	public String execute() {

		if(fileQueue != null && fileQueue.size() >0){
			fileQueue.get(0);
		}
		return SUCCESS;
	}

	public List<FileInfo> fileQueue;

	public List<FileInfo> getFileQueue() {
		return fileQueue;
	}
	@TypeConversion(converter = "com.tdqs.pnpms.commons.file.web.convertor.FileListConvertor")
	public void setFileQueue(List<FileInfo> fileQueue) {
		this.fileQueue = fileQueue;
	}

	public List<FileInfo> fileQueue1;

	public List<FileInfo> getFileQueue1() {
		return fileQueue1;
	}
	@TypeConversion(converter = "com.tdqs.pnpms.commons.file.web.convertor.FileListConvertor")
	public void setFileQueue1(List<FileInfo> fileQueue1) {
		this.fileQueue1 = fileQueue1;
	}
	
	/**
	 * @function 获取当前登录用户
	 * @return 当前登录用户信息
	 * @author Dormin
	 */
	public User getCurrentUser(){
		return (User) session.get(SessionKey.SESSION_KEY_USER);
	}
}
